let query = [];
const MINIMUM = 1;
const MAXIMUM = 5_000;

function gen(exclude = []) {
  let idx = 0;
  const max = MAXIMUM - exclude.length;
  const min = MINIMUM;

  let gen = Math.floor(Math.random() * (max - min + 1)) + min;

  while (exclude[idx] <= gen && idx < exclude.length) {
    idx += 1;
    gen += 1;
  }

  return gen;
}

// for (let i = 0; i < 10; i++) {
//   const startTime_MEM = performance.now();
//   const query_all = Array.from({ length: MAXIMUM }, (v, k) => k + 1).sort(
//     () => 0.5 - Math.random(),
//   );
//
//   for (let i = 0; i < MAXIMUM; i++) {
//     query_all.shift();
//   }
//   const endTime_MEM = performance.now();
//   console.log(`${endTime_MEM - startTime_MEM} `);
// }

for (let i = 0; i < 10; i++) {
  query = [];
  const startTime = performance.now();
  for (let i = 0; i < MAXIMUM; i++) {
    query.push(gen(query));
    query.sort((a, b) => a - b);
  }
  const endTime = performance.now();

  console.log(`${endTime - startTime}`);
}

// const queue = [];
// let queueMaxLength = MAXIMUM;
//
// const getTicket = () => {
//   if (queue.length > queueMaxLength) {
//     return null;
//   }
//
//   let index = Math.floor(Math.random() * (queueMaxLength - queue.length));
//   let delta = 0;
//
//   while (true) {
//     const leftIndex = queue.findLastIndex((i) => i <= index + delta);
//     delta = leftIndex + 1;
//
//     if (queue.indexOf(index + delta) === -1) {
//       queue.push(index + delta);
//       queue.sort((a, b) => a - b);
//       return index + delta;
//     }
//   }
// };
//
// const startTime = performance.now();
// for (let i = 0; i <= queueMaxLength; i += 1) {
//   getTicket();
// }
// const endTime = performance.now();
//
// console.log(`${endTime - startTime}`);
